import React, { Component } from 'react'
import { doLogin } from 'api'
import { Form, Input, Button, Message } from 'antd';
// 登录 actionCreators
import { doLoginAsync } from '../../store/user/actionCreators.js'
import { connect } from 'react-redux'
// 解析search函数
import { parseParams } from 'utils'
const layout = {
  labelCol: {
    span: 8,
  },
  wrapperCol: {
    span: 16,
  },
};
const tailLayout = {
  wrapperCol: {
    offset: 8,
    span: 16,
  },
};

class Login extends Component {
	render() {
		return (
			<div>
				 <Form
				      {...layout}
				      name="basic"
				      initialValues={{
				        remember: true,
				      }}
				      onFinish={this.Login}
				     
				    >
				      <Form.Item
				        label="用户名"
				        name="username"
				        rules={[
				          {
				            required: true,
				            message: '请填写用户名!',
				          },
				        ]}
				      >
				        <Input />
				      </Form.Item>
				
				      <Form.Item
				        label="密码"
				        name="pwd"
				        rules={[
				          {
				            required: true,
				            message: '请填写密码',
				          },
				        ]}
				      >
				        <Input.Password />
				      </Form.Item>
				
				      <Form.Item {...tailLayout}>
				        <Button type="primary" htmlType="submit">
				          登录
				        </Button>
				      </Form.Item>
				    </Form>
			</div>
		)
	}
  Login = (value)=>{
    /* 
    重定向到登录时，携带 state state 中有一个from 就是从哪来
    */
    // 登录
    let fromUrl = "/admin/dashboard";
    // 判断重定向 有没有携带state from
    // console.log(parseParams(this.props))
    if( this.props.location.state ){
      fromUrl = this.props.location.state.from
    }
    this.props.doLogin(value, fromUrl);
  }
}

const mapStateToProps = ()=>{
  return {}
}
const mapDispatchToProps = (dispatch) =>{
  return {
    doLogin: (params, fromUrl)=>{
      dispatch(doLoginAsync(params, fromUrl))
    }
  }
}

export default connect(mapStateToProps,mapDispatchToProps)(Login)